<template>
	<mumu-get-qrcode @success='qrcodeSucess' :definition="true" @error="qrcodeError"></mumu-get-qrcode>
</template>

<script>
	import mumuGetQrcode from '@/uni_modules/mumu-getQrcode/components/mumu-getQrcode/mumu-getQrcode.vue'
	export default {
		components: {
			mumuGetQrcode
		},
		data() {
			return {
				listid: "",
				paraString: "",
				shop_id: "",
				listshop_id: ""
			};
		},
		onLoad(options) {
			this.listid = options.id
			this.listshop_id = options.shop_id
		},
		methods: {
			async qrcodeSucess(data) {
				this.paraString = data
				let index = data.indexOf("id"); // 獲取指定字符的位置
				let result = data.substring(index + 3); // 截取指定字符後面的内容
				let shop_id = result.split("&")[0]
				if (shop_id != this.listshop_id) {
					this.http.toast("請檢查二維碼是否正確！")
					return false
				}
				let res = await this.http.request("/api/Ordermake/toOrdermake", 'POST', {
					id: this.listid
				})
				if (res.code == 1) {
					this.http.toast("成功")
					setTimeout(() => {
						uni.navigateBack(1)
					}, 1000)
				} else {
					this.http.toast(res.msg);
				}
			},
			getQueryVariable(variable) {
				var query = this.paraString;
				var rooms = query.split('?');
				for (var i = 0; i < rooms.length; i++) {
					var pair = rooms[i].split('=');
					if (pair[0] == variable) {
						return (pair[1].split('&'))[0]
					}
				}
				//或者拼接&後面的值
				var vars = query.split('&')
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split('=')
					if (pair[0] == variable) {
						return pair[1]
					}
				}
			},
			qrcodeError(err) {
				console.log(err)
				uni.showModal({
					title: '攝像頭授權失敗',
					content: '攝像頭授權失敗，請检测当前瀏覽器是否有攝像頭權限。',
					success: () => {
						uni.navigateBack({})
					}
				})
			},
		},
	};
</script>

<style></style>